/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "spatial_device_info.h"
#include "string_ex.h"
#include "spatial_awareness_common.h"

namespace OHOS {
namespace Msdp {
SpatialDeviceInfo::SpatialDeviceInfo(const std::string &deviceId,
    const std::string deviceName, const SpatialDeviceType &type) : deviceId_(deviceId),
    deviceName_(deviceName), deviceTypeId_(type)
{
}

/**
 * @brief read this Sequenceable object from a Parcel.
 *
 * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
 * @return Returns true if read successed; returns false otherwise.
 */
bool SpatialDeviceInfo::ReadFromParcel(Parcel &parcel)
{
    deviceId_ = Str16ToStr8(parcel.ReadString16());
    deviceName_ = Str16ToStr8(parcel.ReadString16());
    deviceTypeId_ = static_cast<SpatialDeviceType>(parcel.ReadUint32());
    return true;
}

/**
 * @brief Unmarshals this Sequenceable object from a Parcel.
 *
 * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
 */
SpatialDeviceInfo *SpatialDeviceInfo::Unmarshalling(Parcel &parcel)
{
    SpatialDeviceInfo *deviceInfo = new (std::nothrow) SpatialDeviceInfo();
    if (deviceInfo && !deviceInfo->ReadFromParcel(parcel)) {
        SPATIAL_LOGI(MODULE_INNERKIT, "SpatialDeviceInfo::Unmarshalling ReadFromParcel failed");
        delete deviceInfo;
        deviceInfo = nullptr;
    }
    return deviceInfo;
}

/**
 * @brief Marshals this Sequenceable object into a Parcel.
 *
 * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
 */
bool SpatialDeviceInfo::Marshalling(Parcel &parcel) const
{
    return (parcel.WriteString16(Str8ToStr16(deviceId_)) &&
        parcel.WriteString16(Str8ToStr16(deviceName_)) && parcel.WriteInt32(static_cast<uint32_t>(deviceTypeId_)));
}
}  // namespace Msdp
}  // namespace OHOS